package com.anjho.pojo;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="client")
@NamedQueries(value = {
		@NamedQuery(name = "Client.getClientList", query = "SELECT c FROM Client c"),
		@NamedQuery(name = "Client.getClientByClcode", query = "SELECT c FROM Client c WHERE c.clcode = :clcode")
})
public class Client implements Serializable {
	
	private static final long serialVersionUID = -341935374393970880L;
	
	public Client(){
		this.setPerson(new Person());
	}

	@Id
	@Column(name="clcode")
	@OneToMany(mappedBy="client", cascade = {CascadeType.PERSIST})
	@GeneratedValue(strategy=GenerationType.SEQUENCE)
	private Long clcode;
	
	@OneToOne(cascade = {CascadeType.PERSIST})
	@JoinColumn(name="document", nullable=false, referencedColumnName="document")
	private Person person;

	public Long getClcode() {
		return clcode;
	}

	public void setClcode(Long clcode) {
		this.clcode = clcode;
	}
	
	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}
	
	public String getFormattedDocument() {
		return this.person.getDocument().substring(0, 3) + "."
				+ this.person.getDocument().substring(3, 6) + "."
				+ this.person.getDocument().substring(6, 9) + "-"
				+ this.person.getDocument().substring(9, 11);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((clcode == null) ? 0 : clcode.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Client other = (Client) obj;
		if (clcode == null) {
			if (other.clcode != null)
				return false;
		} else if (!clcode.equals(other.clcode))
			return false;
		return true;
	}
	
}
